/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.draggable;

import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * 
 * @author Benoit Bouchez
 */
public class JQDraggableUtils
{

	/**
	 * Create a JQDraggableBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @param key
	 *            plugin unique id on which component is attached
	 * @return JQDraggableBuilder
	 */
	public static JQDraggableBuilder create(String id, String key)
	{
		return new JQDraggableBuilder(new JQDraggable(id, (key == null ? JQDraggablePlugin.DEFAULT_KEY : key)));
	}

	/**
	 * Create a JQDraggableBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @return JQDraggableBuilder
	 */
	public static JQDraggableBuilder create(String id)
	{
		return new JQDraggableBuilder(new JQDraggable(id, null));
	}

	/**
	 * @author Benoit Bouchez
	 */
	public static final class JQDraggableBuilder
	{
		/** JQuery component */
		private JQDraggable component;
		/** JQuery default options */
		private JQDraggableOptions options;

		/**
		 * Constructor
		 * 
		 * @param component
		 *            widget
		 */
		public JQDraggableBuilder(JQDraggable component)
		{
			this.component = component;
			this.options = new JQDraggableOptions();
		}

		/**
		 * Sets plugin init mode
		 * 
		 * @param initMode
		 *            plugin init mode
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder initMode(int initMode)
		{
			component.getPlugin().setInitMode(initMode);
			return this;
		}

		/**
		 * Sets plugin selector when initMode is PLUGIN_INIT_MODE_ONCE
		 * 
		 * @param initOnceSelector
		 *            JQuery selector
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder initOnceSelector(String initOnceSelector)
		{
			component.getPlugin().setInitOnceSelector(initOnceSelector);
			return this;
		}

		/**
		 * Sets the appendTo option value
		 * 
		 * @param appendTo
		 *            draggable appendTo
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder appendTo(String appendTo)
		{
			options.setAppendTo(appendTo);
			return this;
		}

		/**
		 * Sets the axis option value
		 * 
		 * @param axis
		 *            draggable axis
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder axis(String axis)
		{
			options.setAxis(axis);
			return this;
		}

		/**
		 * Sets the cancel option value
		 * 
		 * @param cancel
		 *            draggable cancel
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder cancel(String cancel)
		{
			options.setCancel(cancel);
			return this;
		}

		/**
		 * Sets the containment option value
		 * 
		 * @param containment
		 *            draggable containment
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder containment(String containment)
		{
			options.setContainment(containment);
			return this;
		}

		/**
		 * Sets the cursor option value
		 * 
		 * @param cursor
		 *            draggable cursor
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder cursor(String cursor)
		{
			options.setCursor(cursor);
			return this;
		}

		/**
		 * Sets the cursorAt option value
		 * 
		 * @param cursorAt
		 *            draggable cursorAt
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder cursorAt(String cursorAt)
		{
			options.setCursorAt(cursorAt);
			return this;
		}

		/**
		 * Sets the delay option value
		 * 
		 * @param delay
		 *            draggable delay
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder delay(int delay)
		{
			options.setDelay(delay);
			return this;
		}

		/**
		 * Sets the distance option value
		 * 
		 * @param distance
		 *            draggable distance
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder distance(int distance)
		{
			options.setDistance(distance);
			return this;
		}

		/**
		 * Sets the grid option value
		 * 
		 * @param grid
		 *            draggable grid
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder grid(String grid)
		{
			options.setGrid(grid);
			return this;
		}

		/**
		 * Sets the handle option value
		 * 
		 * @param handle
		 *            draggable handle
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder handle(String handle)
		{
			options.setHandle(handle);
			return this;
		}

		/**
		 * Sets the helper option value
		 * 
		 * @param helper
		 *            draggable helper
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder helper(String helper)
		{
			options.setHelper(helper);
			return this;
		}

		/**
		 * Sets the opacity option value
		 * 
		 * @param opacity
		 *            draggable opacity
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder opacity(float opacity)
		{
			options.setOpacity(opacity);
			return this;
		}

		/**
		 * Sets the revert option value
		 * 
		 * @param revert
		 *            draggable revert
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder revert(boolean revert)
		{
			options.setRevert(revert);
			return this;
		}

		/**
		 * Sets the scroll option value
		 * 
		 * @param scroll
		 *            draggable scroll
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder scroll(boolean scroll)
		{
			options.setScroll(scroll);
			return this;
		}

		/**
		 * Sets the scroll sensitivity option value
		 * 
		 * @param scrollSensitivity
		 *            draggable scroll sensitivity
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder scrollSensitivity(int scrollSensitivity)
		{
			options.setScrollSensitivity(scrollSensitivity);
			return this;
		}

		/**
		 * Sets the scroll speed option value
		 * 
		 * @param scrollSpeed
		 *            draggable scroll speed
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder scrollSpeed(int scrollSpeed)
		{
			options.setScrollSpeed(scrollSpeed);
			return this;
		}

		/**
		 * Sets the snap option value
		 * 
		 * @param snap
		 *            draggable snap
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder snap(boolean snap)
		{
			options.setSnap(snap);
			return this;
		}

		/**
		 * Sets the snap option value
		 * 
		 * @param snap
		 *            draggable snap
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder snap(String snap)
		{
			options.setSnap(snap);
			return this;
		}

		/**
		 * Sets the snapMode option value
		 * 
		 * @param snapMode
		 *            draggable snap mode
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder snapMode(String snapMode)
		{
			options.setSnapMode(snapMode);
			return this;
		}

		/**
		 * Sets the snapTolerance option value
		 * 
		 * @param snapTolerance
		 *            draggable snapTolerance
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder snapTolerance(int snapTolerance)
		{
			options.setSnapTolerance(snapTolerance);
			return this;
		}

		/**
		 * Sets the zIndex option value
		 * 
		 * @param index
		 *            draggable zIndex
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder zIndex(int index)
		{
			options.setZIndex(index);
			return this;
		}

		/**
		 * Sets the start function
		 * 
		 * @param startScript
		 *            draggable start function
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder startScript(String startScript)
		{
			options.setStartScript(startScript);
			return this;
		}

		/**
		 * Sets the drag function
		 * 
		 * @param dragScript
		 *            draggable drag function
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder dragScript(String dragScript)
		{
			options.setDragScript(dragScript);
			return this;
		}

		/**
		 * Sets the stop function
		 * 
		 * @param stopScript
		 *            draggable stop function
		 * @return JQDraggableBuilder
		 */
		public JQDraggableBuilder stopScript(String stopScript)
		{
			options.setStopScript(stopScript);
			return this;
		}

		/**
		 * @return JQDraggable
		 */
		public JQDraggable build()
		{
			if (component.getPlugin().getInitMode() == JQPlugin.PLUGIN_INIT_ONCE)
				component.getPlugin().setOptions(options);
			else
				component.setOptions(options);
			return component;
		}
	}
}